package cn.edu.seu.herald.informationplatform.dao.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
import cn.edu.seu.herald.informationplatform.bean.league.Recruit;

public class RecruitDao extends DataAccessObject{
	private static RecruitDao daoInstance=null;
	protected RecruitDao(){
	}
	public static RecruitDao getDao(){
		if(daoInstance==null){
			daoInstance=new RecruitDao();
		}
		return daoInstance;
	}
	
	//add
	public int addRecuit(Recruit recruit) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("insert into lg_recruit(league_id,information,contact,release_time,attachment_address) values (?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1,recruit.getLegueId());
			ps.setString(2,recruit.getInformation());
			ps.setString(3,recruit.getContact());
			ps.setDate(4,recruit.getReleaseTime());
			ps.setString(5,recruit.getAttachmentAddress());
			ps.executeUpdate();
			ResultSet rsResultSet=ps.getGeneratedKeys();
			
			int new_id=-1;
			if(rsResultSet.next()){
				new_id=rsResultSet.getInt(1);
			}
			closeRs(rsResultSet);
			return new_id;
	    	}catch (SQLException e){
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//delete
	public void deleteRecuitByLeagueId(int leagueId) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("delete from lg_recruit where league_id=?");
			ps.setInt(1,leagueId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	
	//delete
	public void deleteRecuit(int id) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("delete from lg_recruit where id=?");
			ps.setInt(1,id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	//update
	public void updateRecuit(Recruit recruit) throws SQLException{
		Connection con=getConnectionLeague();
		try {
			PreparedStatement ps=con.prepareStatement("update lg_recruit set information=?,contact=?,release_time=?,attachment_address=? where league_id=?");
			ps.setString(1,recruit.getInformation());
			ps.setString(2,recruit.getContact());
			ps.setDate(3,recruit.getReleaseTime());
			ps.setString(4,recruit.getAttachmentAddress());
			ps.setInt(5, recruit.getLegueId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		finally{
			closeCon(con);
		}
	}
	//select by leagueId return object of Recruit
	public Recruit getRecruitById(int leagueId ) throws SQLException{
		Connection con=getConnectionLeague();
    	try{
    	PreparedStatement ps=con.prepareStatement("select * from lg_recruit where league_id=? ");
    	ps.setInt(1,leagueId);
    	ResultSet rsResultSet=ps.executeQuery();
    	Recruit recruit=new Recruit();
    	while(rsResultSet.next()){
    		recruit.setInformation(rsResultSet.getString(3));
    		recruit.setContact(rsResultSet.getString(4));
    		recruit.setReleaseTime(rsResultSet.getDate(5));
    		recruit.setAttachmentAddress(rsResultSet.getString(6));
    	}
    	closeRs(rsResultSet);
    	return recruit;
    	}catch (SQLException e){
		e.printStackTrace();
		throw e;
	}
	finally{
		closeCon(con);
	}
	}
	
}
